package testes.vSys.action;

//import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import testes.vSys.bean.Moviment;
import testes.vSys.dao.DAOFactory;
import testes.vSys.dao.IMovimentDao;
import testes.vSys.form.MovimentFinancesReportForm;



public class MovimentFinancesReportAction extends DispatchAction{

	private final String MOVIMENT_COLLECTION = "movimentCollection";
		
	public ActionForward load(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		return mapping.findForward("movimentFinancesReport");
	}

	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

			MovimentFinancesReportForm movimentForm = (MovimentFinancesReportForm)form;
		
			System.out.println(movimentForm.getOpr());
		
			IMovimentDao movimentDAO = DAOFactory.getMovimentDao();
	
			DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
			
			Date dateBegin = formatter.parse(movimentForm.getDateBegin());
			Date dateEnd = formatter.parse(movimentForm.getDateEnd());
			
		if((!dateBegin.equals(null)) && (!dateEnd.equals(null)) && (dateBegin.compareTo(dateEnd) < 0 ))
		{
			Collection<Moviment> moviment = movimentDAO.getMovimentBetween(dateBegin, dateEnd);
			request.setAttribute(MOVIMENT_COLLECTION, moviment);
			request.setAttribute("show", true);
		}
		else
		{
			request.setAttribute("error", true);
		}
		return load(mapping, form, request, response);
			
	}
}

